package com.midea.ai.air.ui.user.fragment;

import android.app.Dialog;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.home.MainActivityNew;
import com.midea.ai.air.ui.user.ForgetPasswordActivity;
import com.midea.ai.air.ui.user.LoginActivity;
import com.midea.ai.air.utils.StringUtil;
import com.midea.msmartsdk.openapi.MSmartListener;
import com.midea.msmartsdk.openapi.MSmartSDK;

/**
 * Created by hww on 2016/3/22.
 */
public class LogInFragment extends Fragment implements View.OnClickListener {

    private Button log_in_bt;
    private TextView log_in_forgetpassword, log_in_experience;
    private ImageView phone_img, password_img;
    private EditText phone_ed, password_ed;
    private Dialog loading_Dialog;


    private static final int LOGIN_SECCESS = 0X110;


    Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case LOGIN_SECCESS:
                    if (loading_Dialog != null) {
                        loading_Dialog.dismiss();
                    }
                    startActivity(new Intent(getActivity(), MainActivityNew.class));
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_log_in, container, false);

        initView(view);

        return view;
    }


    private void initView(View view) {

        log_in_bt = (Button) view.findViewById(R.id.log_in_bt);
        log_in_forgetpassword = (TextView) view.findViewById(R.id.log_in_forget_password);
        log_in_experience = (TextView) view.findViewById(R.id.log_in_experience);
        phone_ed = (EditText) view.findViewById(R.id.log_in_phone);
        password_ed = (EditText) view.findViewById(R.id.log_in_password);
        phone_img = (ImageView) view.findViewById(R.id.log_in_imageView_phone);
        password_img = (ImageView) view.findViewById(R.id.log_in_imageView_password);

        log_in_bt.setOnClickListener(this);
        log_in_forgetpassword.setOnClickListener(this);
        log_in_experience.setOnClickListener(this);
        log_in_bt.setAlpha(0.25f);

        loading_Dialog = LoginActivity.createLoadingDialog(getActivity(), R.string.dialog_loading);

        //登陆按钮激活
        password_ed.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                LoginActivity.mLog("beforeTextChanged = " + s.toString() + "::" + start + "::" + count + "::" + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LoginActivity.mLog("onTextChanged = " + s.toString() + "::" + start + "::" + before + "::" + count);
                if (!TextUtils.isEmpty(phone_ed.getText())) {
                    if (count < 0) {
                        log_in_bt.setAlpha(0.25f);
                    } else {
                        log_in_bt.setAlpha(1f);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                LoginActivity.mLog("afterTextChanged = " + s.toString());
            }
        });


        //输入框焦点，判断是否显示图标
        phone_ed.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    phone_img.setVisibility(View.VISIBLE);
                } else {
                    phone_img.setVisibility(View.INVISIBLE);
                }
            }
        });
        password_ed.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    password_img.setVisibility(View.VISIBLE);
                } else {
                    password_img.setVisibility(View.INVISIBLE);
                }
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.log_in_bt:
                //登陆按钮
                log_in();
                break;
            case R.id.log_in_forget_password:
                //登陆遇到问题
                startActivity(new Intent(getActivity(), ForgetPasswordActivity.class));
                break;
            case R.id.log_in_experience:
                //体验虚拟操作

                break;

            default:
                break;
        }

    }


    //登陆方法
    private void log_in() {
        String phone = phone_ed.getText().toString().trim();
        String password = password_ed.getText().toString().trim();


        if (TextUtils.isEmpty(phone)) {
            LoginActivity.myToast(getActivity(), R.string.log_in_phone_empty);
            return;
        }
        if (TextUtils.isEmpty(password)) {
            LoginActivity.myToast(getActivity(), R.string.log_in_password_empty);
            return;
        }

        if (phone.length() != 11) {
            LoginActivity.myToast(getActivity(), R.string.log_in_phone_eleven);
            return;
        }

        if (!StringUtil.isMobileNO(phone)) {
            LoginActivity.myToast(getActivity(), R.string.log_in_phone_error);
            return;
        }

        if (password.length() < 6 || password.length() > 16) {
            LoginActivity.myToast(getActivity(), R.string.register_password_error);
            return;
        }

        if (LoginActivity.isSpecialCharacter(password)) {
            LoginActivity.myToast(getActivity(), R.string.register_password_error);
            return;
        }

        loading_Dialog.show();

        MSmartSDK.getInstance().getUserManager().loginWithAccount(phone, password, new MSmartListener() {

            @Override
            public void onComplete() {
                Toast.makeText(getActivity().getApplicationContext(), "Login Success", Toast.LENGTH_SHORT).show();
                handler.sendEmptyMessageDelayed(LOGIN_SECCESS, 1800);
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(getActivity().getApplicationContext(), "登录失败:" + s, Toast.LENGTH_SHORT).show();
            }
        });

    }
}
